<?php require ('setcookie.html'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="de">
<head>
    <title>MP3 Vokabeltrainer - Vokabeln sortieren - Lingomoto</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="description" content="MP3 Vokabeltrainer - Vokabeln sortieren - Sprachen lernen" />
    <meta name="language" content="de" />
    <link href="style.css" type="text/css" rel="stylesheet" />
    <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
    <script language="JavaScript">function sprung(ziel) {window.location.href=ziel;}</script>
    <script type="text/javascript">
        <!--
        function test(state){
        var checkboxes=document.getElementsByName('chosen_words[]');
        for(var n=0;n<checkboxes.length;n++){
        checkboxes[n].checked=state;
        }
        }
        -->
    </script>
</head>

<body id="trainer">

<?php
$_SESSION['logini'] = isset($_SESSION['logini']) ? $_SESSION['logini'] : false;
// read configuration
require ('konfiguration.html');
// read user-data
require ('nutzer.html');
// read header
$link = "vokabeln-sortieren.html";
$langen = 'classify-words.html';
$langes = 'clasificar-palabras.html';
$langru = 'classify-words.html';
require ('kopf.html');
// <!-- horizontal column -->
echo '<div>';
require ('horizontal.html');
echo '</div>';
// <!-- Left Column -->
echo '<div id="left_col">';
require ('vertikal.html');
echo '</div>';

// <!-- Content -->
echo '<div id="content">';
echo '<div id="block5">';

// request for login
if ($_SESSION['logini']== false) {
    require_once ('loginformular.html'); 
}
 
// if new password, request for change of password
elseif ( $newpass == 1 )
{
$passwo = isset($_REQUEST['passwo']) ? $_REQUEST['passwo'] : '1';
$passwo1 = isset($_REQUEST['passwo1']) ? $_REQUEST['passwo1'] : '1';
$passwo2 = isset($_REQUEST['passwo2']) ? $_REQUEST['passwo2'] : '1';
$ok='';
require_once ('neues-passwort.html');
}
else {
// initialize variables
$customerid = $_SESSION['customerid'];
if ( $_SESSION['langpair'] == '' ) { $langpair = '0'; } else { $langpair = $_SESSION['langpair']; }
$change_new = isset($_REQUEST['change_new']) ? $_REQUEST['change_new'] : '';
$change_old = isset($_REQUEST['change_old']) ? $_REQUEST['change_old'] : '';
if ( $change_new <> '' ) { require_once ('newlang.html'); }
if ( $change_old <> '' ) { require_once ('oldlang.html'); }
$action = '';
$chosen_words = isset($_REQUEST['chosen_words']) ? $_REQUEST['chosen_words'] : array();
$wordbox_new = isset($_REQUEST['wordbox_new']) ? $_REQUEST['wordbox_new'] : '';
$checked = isset($_REQUEST['savechecked']) ? $_REQUEST['savechecked'] : 'wordbox0';
$boxbefore = isset($_REQUEST['boxbefore']) ? $_REQUEST['boxbefore'] : 0;

if ( $checked == 'wordbox0' ) { $sql = "SELECT * FROM wordbox0 where boxid = 0 and customerid =". $customerid . " and langpairid =". $langpair . " order by 1word,2word,3word"; }
if ( $checked == 'wordbox1' ) { $sql = "SELECT * FROM wordbox0 where boxid = 1 and customerid =". $customerid . " and langpairid =". $langpair . " order by 1word,2word,3word"; }
if ( $checked == 'wordbox2' ) { $sql = "SELECT * FROM wordbox0 where boxid = 2 and customerid =". $customerid . " and langpairid =". $langpair . " order by 1word,2word,3word"; }
if ( $checked == 'wordbox3' ) { $sql = "SELECT * FROM wordbox0 where boxid = 3 and customerid =". $customerid . " and langpairid =". $langpair . " order by 1word,2word,3word"; }
if ( $checked == 'wordbox4' ) { $sql = "SELECT * FROM wordbox0 where boxid = 4 and customerid =". $customerid . " and langpairid =". $langpair . " order by 1word,2word,3word"; }
if ( $checked == 'wordbox5' ) { $sql = "SELECT * FROM wordbox0 where boxid = 5 and customerid =". $customerid . " and langpairid =". $langpair . " order by 1word,2word,3word"; }
if ( $checked == 'wordbox6' ) { $sql = "SELECT * FROM wordbox0 where boxid = 6 and customerid =". $customerid . " and langpairid =". $langpair . " order by 1word,2word,3word"; }

if (isset($_REQUEST['wordbox_new']))   { $action = 'sortvoc'; }
if (isset($_REQUEST['delete_x']))   { $action = 'delvoc'; }

if (isset($_REQUEST['submit0_x'])) { $sql = "SELECT * FROM wordbox0 where boxid = 0 and customerid =". $customerid . " and langpairid =". $langpair . " order by 1word,2word,3word"; $checked = 'wordbox0'; $action = ''; }
if (isset($_REQUEST['submit1_x'])) { $sql = "SELECT * FROM wordbox0 where boxid = 1 and customerid =". $customerid . " and langpairid =". $langpair . " order by 1word,2word,3word"; $checked = 'wordbox1'; $action = ''; }
if (isset($_REQUEST['submit2_x'])) { $sql = "SELECT * FROM wordbox0 where boxid = 2 and customerid =". $customerid . " and langpairid =". $langpair . " order by 1word,2word,3word"; $checked = 'wordbox2'; $action = ''; }
if (isset($_REQUEST['submit3_x'])) { $sql = "SELECT * FROM wordbox0 where boxid = 3 and customerid =". $customerid . " and langpairid =". $langpair . " order by 1word,2word,3word"; $checked = 'wordbox3'; $action = ''; }
if (isset($_REQUEST['submit4_x'])) { $sql = "SELECT * FROM wordbox0 where boxid = 4 and customerid =". $customerid . " and langpairid =". $langpair . " order by 1word,2word,3word"; $checked = 'wordbox4'; $action = ''; }
if (isset($_REQUEST['submit5_x'])) { $sql = "SELECT * FROM wordbox0 where boxid = 5 and customerid =". $customerid . " and langpairid =". $langpair . " order by 1word,2word,3word"; $checked = 'wordbox5'; $action = ''; }
if (isset($_REQUEST['submit6_x'])) { $sql = "SELECT * FROM wordbox0 where boxid = 6 and customerid =". $customerid . " and langpairid =". $langpair . " order by 1word,2word,3word"; $checked = 'wordbox6'; $action = ''; }


// DB Query
$db_res = mysql_query( $sql );
if ( ! $db_res ) { die('Error query: ' . mysql_error()); }
                                                
// execute function: show buttons for wordboxes 
switch ( $action ) {
  case 'sortvoc' :  sortvoc();
  case 'delvoc' : delvoc();
  $db_res = mysql_query( $sql );
  if ( ! $db_res ) { die('Error query: ' . mysql_error()); }
  }

show_dialog();

}

// ----------------------- Functions --------------------------------------------------------------------------------------------

function show_dialog() {

  global $checked,$db_res,$customerid;

  echo '<form name="xxx" action="vokabeln-sortieren.html" method="POST">';

    echo '<h2>Vokabeln sortieren</h2>';

    // Sprachen auslesen
    $sql2 = "SELECT can,learn,langpair FROM settings where customerid = '$customerid'";
    mysql_query('SET NAMES utf8');
    mysql_query('SET CHARACTER SET utf8');
    $db_sel2 = mysql_query( $sql2 );
    if ( ! $db_sel2 ) { die('Error query 1: ' . mysql_error()); }
    while ($row = mysql_fetch_array( $db_sel2, MYSQL_ASSOC)) {
    if ( $row['can'] == '' or $row['learn'] == '' or $row['can'] == '-' or $row['learn'] == '-' )
    {
        echo '<br />';
        $missinglangpair = '1';
        require_once ('sprache-waehlen.html');
    }
    else
    {
    echo '<div id="block19">';
    echo '<br />';

    if ( $checked == 'wordbox0' ) { 
    echo '<input type="image" src="bilder/buttons/nlernbox.gif" name="submit0" alt="Lernbox" title=" Lernbox " />';
    } else {
    echo '<input type="image" src="bilder/buttons/nlernboxb.gif" name="submit0" alt="Lernbox" title=" Lernbox " />';
    }
    if ( $checked == 'wordbox1' ) {
    echo '<input type="image" src="bilder/buttons/nbox1.gif" name="submit1" alt="Box 1" title=" Box 1 " class="button7" />';
    } else {
    echo '<input type="image" src="bilder/buttons/nbox1b.gif" name="submit1" alt="Box 1" title=" Box 1 " class="button7" />';
    }
    if ( $checked == 'wordbox2' ) {
    echo '<input type="image" src="bilder/buttons/nbox2.gif" name="submit2" alt="Box 2" title=" Box 2 " class="button7" />';
    } else {
    echo '<input type="image" src="bilder/buttons/nbox2b.gif" name="submit2" alt="Box 2" title=" Box 2 " class="button7" />';
    }
    if ( $checked == 'wordbox3' ) {
    echo '<input type="image" src="bilder/buttons/nbox3.gif" name="submit3" alt="Box 3" title=" Box 3 " class="button7" />';
    } else {
    echo '<input type="image" src="bilder/buttons/nbox3b.gif" name="submit3" alt="Box 3" title=" Box 3 " class="button7" />';
    }
    if ( $checked == 'wordbox4' ) {
    echo '<input type="image" src="bilder/buttons/nbox4.gif" name="submit4" alt="Box 4" title=" Box 4 " class="button7" />';
    } else {
    echo '<input type="image" src="bilder/buttons/nbox4b.gif" name="submit4" alt="Box 4" title=" Box 4 " class="button7" />';
    }
    if ( $checked == 'wordbox5' ) {
    echo '<input type="image" src="bilder/buttons/nbox5.gif" name="submit5" alt="Box 5" title=" Box 5 " class="button7" />';
    } else {
    echo '<input type="image" src="bilder/buttons/nbox5b.gif" name="submit5" alt="Box 5" title=" Box 5 " class="button7" />';
    }
    if ( $checked == 'wordbox6' ) {
    echo '<input type="image" src="bilder/buttons/nbox6.gif" name="submit6" alt="Box 6" title=" Box 6 " class="button7" />';
    } else {
    echo '<input type="image" src="bilder/buttons/nbox6b.gif" name="submit6" alt="Box 6" title=" Box 6 " class="button7" />';
    }
    echo '<div class="line2" >.</div><br />';

    echo '<table border="0" style="border-collapse:collapse">';
    echo '<tr>';    
    echo '<td>';
    echo '<input type="checkbox" name="checkall" value="1" onclick="test(this.checked)" />';
    echo '</td>';
    echo '<td class="textmedium" >';
    echo '&nbsp;&nbsp;&nbsp;alle';
    echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
    echo '</td>';
 //   echo '<input type="submit" value="Vokabeln sortieren" name="sortvoc">';

    // Show listbox with wordboxes
    echo '<td>';
    echo '<select name="wordbox_new" onchange=xxx.submit()>';
        echo '<option value="" >sortieren nach&nbsp;</option>';
        echo '<option value="0" >Lernbox&nbsp;</option>';
        echo '<option value="1" >Box 1&nbsp;</option>';
        echo '<option value="2" >Box 2&nbsp;</option>';
        echo '<option value="3" >Box 3&nbsp;</option>';
        echo '<option value="4" >Box 4&nbsp;</option>';
        echo '<option value="5" >Box 5&nbsp;</option>';
        echo '<option value="6" >Box 6&nbsp;</option>';
    echo '</select>';
    echo '</td>';
    echo '<td>';
    echo '<input type="image" src="bilder/buttons/loeschen.gif" class="button4" name="delete" alt="L&ouml;schen" title=" L&ouml;schen " />';
    echo '<input type=hidden name=savechecked value='. $checked . ' />';
    echo '</td>';
    echo '</tr>';
    echo '</table>';
    echo '<br />';

    echo '<div class="heading">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ausgangsw&ouml;rter</div>';
    echo '<div class="heading2">&Uuml;bersetzungen</div>';
    if ( $checked == 'wordbox0' ) {
        echo '<div class="heading9">Aus Box</div>';
    }
    else
    {
        echo '<div class="heading9">&nbsp;</div>';
    }
    echo '<br />';
    
    $h = 0;
    
    echo '<table border="0" style="border-collapse:collapse">';
    while ($row = mysql_fetch_array( $db_res, MYSQL_ASSOC)) {

        $key = $row['customerid'] . ',' . $row['boxid'] . ',' . $row['langpairid'] . ',' . $row['langid'] . ',' . $row['1word'] . ',' . $row['2word'] . ',' . $row['3word'] . ',' . $row['word1'] . ',' . $row['word2'] . ',' . $row['word3'] . ',' . $row['timestamp'] ;

        if ( $h == 0 ) { echo '<tr>'; } else { echo '<tr class="bg">'; }
            echo "<td class='check6'><input type=Checkbox name=chosen_words[] value='". $key ."' /></td>";
            echo "<td class='table6'>". $row['1word'] . "</td>";
      if ( $row['2word'] == '' )
        { echo "<td class='table6'>&nbsp;-</td>"; }
      else
        { echo "<td class='table6'>". $row['2word'] . "</td>"; }
      if ( $row['3word'] == '' )
        { echo "<td class='table6'>&nbsp;-</td>"; }
      else
        { echo "<td class='table6'>". $row['3word'] . "</td>"; }

      echo "<td class='table6'>". $row['word1'] . "</td>";
      if ( $row['word2'] == '' )
        { echo "<td class='table6'>&nbsp;-</td>"; }
      else
        { echo "<td class='table6'>". $row['word2'] . "</td>"; }
      if ( $row['word3'] == '' )
        { echo "<td class='table6'>&nbsp;-</td>"; }
      else
        { echo "<td class='table6'>". $row['word3'] . "</td>"; }
        if ( $checked == 'wordbox0' ) {
            echo "<td class='table7'>" . $row['boxbefore'] . "</td>";
        }
        else
        {
            echo "<td class='table7'>&nbsp;</td>";
        }
        // save boxbefore - value
        echo "<input type='hidden' name='boxbefore' value='". $row['boxid'] . "' />";

        echo '</tr>';
        if ( $h == 1 ) { $h = -1; }
        $h = $h + 1;
    }
    echo '</table>';
    
    echo'<br><div id="block20"><a href="neue-vokabelliste.html"> Neue Vokabelliste aus momentaner Lernbox erstellen und für Andere '.
     'verfügbar machen</a></div>  ';
    
    echo '</div>';


    mysql_free_result( $db_res );
    } }
	echo '</form>';
}


// change wordbox of chosen words
function sortvoc() {
  global $chosen_words, $wordbox_new, $key, $boxbefore ;

	 for( $i=0; $i<count($chosen_words); $i++ ) {
		
		// write parts of checkbox-string in $key-array 
		$str = $chosen_words[$i];

        $key = explode(",",$str); 

        // update chosen words
		$sql1 = " update wordbox0 set boxid = ". $wordbox_new .", notselected = 0, boxbefore =". $boxbefore . ",";
		$sql1 .= " timestamp = current_timestamp ";
        $sql1 .= " where customerid = ". $key[0] ." ";
		$sql1 .= " and boxid = ". $key[1] ." and langpairid = ". $key[2] ." and langid = ". $key[3] ." ";
		$sql1 .= " and 1word = '". $key[4] ."' and word1 = '". $key[7] ."' ";
        $db_sel = mysql_query( $sql1 );
        if ( ! $db_sel )
            { die('Error query: ' . mysql_error()); }
        }
} 

function delvoc() {

    global $chosen_words, $key ;

	for( $i=0; $i<count($chosen_words); $i++ ) {

		// write parts of checkbox-string in $key-array
		$str = $chosen_words[$i];

        $key = explode(",",$str);

        // delete chosen words
		$sql1 = " delete from wordbox0 where ";
        $sql1 .= " customerid = ". $key[0] ." and ";
		$sql1 .= " boxid = ". $key[1] . " and langpairid = ". $key[2] . " and langid = ". $key[3] . " ";
		$sql1 .= " and 1word = '". $key[4] . "' and word1 = '". $key[7] . "' ";

		$db_del = mysql_query( $sql1 );
        if ( ! $db_del )
        { die('Error query: ' . mysql_error()); }
    }
}

function mysql_query_secure($query)
{
   $result=mysql_query($query);
   if ( ! $result ) { die('Error query : ' . mysql_error()); }
   return $result;
}

echo '</div>';
echo '</div>';
echo '<div id="adverts">';
echo '<div id="adbox"></div>';
echo '</div>';

require ('fuss.html');
?>

</body>
</html>